using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace UnityEditor.UI
{
    [CustomEditor(typeof(ContentSizeFitter), true)]
    [CanEditMultipleObjects]
    /// <summary>
    /// Custom Editor for the ContentSizeFitter Component.
    /// Extend this class to write a custom editor for a component derived from ContentSizeFitter.
    /// </summary>
    public class ContentSizeFitterEditor : SelfControllerEditor
    {
        SerializedProperty m_HorizontalFit;
        SerializedProperty m_VerticalFit;
        SerializedProperty m_MinWidth;
        SerializedProperty m_MaxWidth;
        SerializedProperty m_MinHeight;
        SerializedProperty m_MaxHeight;
        SerializedProperty m_ExtendParentWidth;
        SerializedProperty m_ExtendParentHeight;
        SerializedProperty m_BorderSize;

        protected virtual void OnEnable()
        {
            m_HorizontalFit = serializedObject.FindProperty("m_HorizontalFit");
            m_VerticalFit = serializedObject.FindProperty("m_VerticalFit");
            m_MinWidth = serializedObject.FindProperty("m_MinWidth");
            m_MaxWidth = serializedObject.FindProperty("m_MaxWidth");
            m_MinHeight = serializedObject.FindProperty("m_MinHeight");
            m_MaxHeight = serializedObject.FindProperty("m_MaxHeight");
            m_ExtendParentWidth = serializedObject.FindProperty("m_ExtendParentWidth");
            m_ExtendParentHeight = serializedObject.FindProperty("m_ExtendParentHeight");
            m_BorderSize = serializedObject.FindProperty("m_BorderSize");
        }

        void ClampSizeGUI(SerializedProperty property)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel(property.displayName);
            var type = property.FindPropertyRelative("clampType");
            var mod = (ContentSizeFitter.ClampMode)EditorGUILayout.EnumPopup((ContentSizeFitter.ClampMode)type.intValue);
            EditorGUILayout.EndHorizontal();
            GUI.changed |= (int)mod != type.intValue;
            type.intValue = (int)mod;
            if (mod != ContentSizeFitter.ClampMode.NotClamp)
            {
                var v = property.FindPropertyRelative("size");
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(v);
                EditorGUI.indentLevel--;
            }
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            EditorGUILayout.PropertyField(m_HorizontalFit, true);
            ClampSizeGUI(m_MinWidth);
            ClampSizeGUI(m_MaxWidth);
            EditorGUILayout.PropertyField(m_ExtendParentWidth);
            //EditorGUILayout.PropertyField(m_MinWidth, true);
            //EditorGUILayout.PropertyField(m_MaxWidth, true);
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(m_VerticalFit, true);
            ClampSizeGUI(m_MinHeight);
            ClampSizeGUI(m_MaxHeight);
            EditorGUILayout.PropertyField(m_ExtendParentHeight);
            EditorGUILayout.PropertyField(m_BorderSize);
            //EditorGUILayout.PropertyField(m_MinHeight, true);
            //EditorGUILayout.PropertyField(m_MaxHeight, true);
            serializedObject.ApplyModifiedProperties();

            base.OnInspectorGUI();
        }
    }
}
